package com.dpj.noLock.atomic;

import lombok.extern.slf4j.Slf4j;
import sun.security.krb5.internal.TGSRep;

import java.util.concurrent.atomic.AtomicReference;


@Slf4j(topic = "c. AtomicReferenceDemo")
public class AtomicReferenceDemo {
    //ABA 问题
    static AtomicReference<String> ref=new AtomicReference<>("A");
    public static void main(String[] args) throws InterruptedException {
        log.debug("main test");
        //获取值A
        String prev = ref.get();
        System.out.println(prev);
        other();
        Thread.sleep(1000);

        //尝试改为c
        log.debug("change A-C {}",ref.compareAndSet(prev,"c"));
    }
    private static void other() throws InterruptedException {
        new Thread(()->{
            log.debug("change A->B {}",ref.compareAndSet(ref.get(), "B"));
        },"t1").start();
        Thread.sleep(10);
        new Thread(()->{
            log.debug("change B-A {}",ref.compareAndSet(ref.get(),"A"));
        },"t2").start();
    }

}
